package com.mjh.zhujianju.service.supervise.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.enums.EStatus;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.supervise.SupervisionScheduleMapper;
import com.mjh.zhujianju.dao.supervise.SupervisionUsersMapper;
import com.mjh.zhujianju.enums.SupervisionScheduleEnum;
import com.mjh.zhujianju.model.supervise.SupervisionEO;
import com.mjh.zhujianju.model.supervise.SupervisionScheduleEO;
import com.mjh.zhujianju.model.supervise.SupervisionUsersEO;
import com.mjh.zhujianju.service.supervise.SupervisionScheduleService;
import com.mjh.zhujianju.service.supervise.SupervisionService;
import com.mjh.zhujianju.service.supervise.SupervisionUsersService;

@Service
public class SupervisionScheduleServiceImpl extends BaseServiceImpl<SupervisionScheduleEO>
		implements SupervisionScheduleService {

	@Resource
	private SupervisionScheduleMapper supervisionScheduleMapper;

	@Resource
	private SysUserService userService;

	@Resource
	private SupervisionService supervisionService;

	@Resource
	private SupervisionUsersService supervisionUsersService;

	@Resource
	private SupervisionUsersMapper supervisionUsersMapper;

	public SupervisionScheduleServiceImpl(SupervisionScheduleMapper supervisionScheduleMapper) {
		super(supervisionScheduleMapper);
	}

	@Override
	@Transactional
	public void saveInfo(SupervisionScheduleEO eo) throws XTBusinessException {
		Integer count = this.getScheduleBySupervisionId(eo);
		if (count != null) {
			throw new XTBusinessException("请修改进度");
		}
		eo.setStatus(EStatus.active.getCode());
		updateSupervisionSchedule(eo);
		this.saveSelective(eo);
	}

	/**
	 * 计算当前整个督办进度
	 * @param eo
	 * @throws XTBusinessException
	 */
	private void updateSupervisionSchedule(SupervisionScheduleEO eo) throws XTBusinessException{
		List<SupervisionScheduleEO> list = supervisionScheduleMapper .selectMaxListBySupervisionId(eo.getSupervisionId());
		if(XTListUtils.isEmpty(list)) {//说明都没参与
			SupervisionEO s = new SupervisionEO();
			s.setSchedule(SupervisionScheduleEnum.startIng.getCode());
			s.setId(eo.getSupervisionId());
			supervisionService.updateByPrimaryKeySelective(s);	
		}
		// 先找人
		List<SupervisionUsersEO> userList = supervisionUsersMapper.selectUserListBysupervisionId(eo.getSupervisionId());
		if (!XTListUtils.isEmpty(userList)) {
			List<Long> userIdLsit = new ArrayList<>();
			for (SupervisionUsersEO supervisionUsersEO : userList) {
				userIdLsit.add(supervisionUsersEO.getUserId());
			}
			// 计算当前督办是否完成
			if (!XTListUtils.isEmpty(list)) {
				Set<Long> userIdSet2 = new HashSet<>();
				Set<Long> userIdLsit2 = new HashSet<>();
				for (SupervisionScheduleEO s : list) {
					userIdSet2.add(s.getUserId());
				}
				userIdSet2.add(eo.getUserId());
				userIdLsit2.addAll(userIdSet2);
				if (userIdLsit.size() == userIdLsit2.size()) {//说明都参与了
					Boolean flag = Boolean.TRUE;
					for (SupervisionScheduleEO supervisionScheduleEO : list) {
						Integer schedule = supervisionScheduleEO.getSchedule();
						if (schedule != 100) {
							flag = Boolean.FALSE;// 说明没有完成的
						}
					}
					if (flag) {// 整个督办事项完成了
						SupervisionEO s = new SupervisionEO();
						s.setSchedule(SupervisionScheduleEnum.endIng.getCode());
						s.setId(eo.getSupervisionId());
						supervisionService.updateByPrimaryKeySelective(s);
					}
				}
			}
		}
	}
	
	
	@Override
	@Transactional
	public void updateInfo(SupervisionScheduleEO eo) throws XTBusinessException {
		Integer count = this.getScheduleBySupervisionId(eo);
		if (count != null) {
			throw new XTBusinessException("请修改进度");
		}
		eo.setStatus(EStatus.active.getCode());
		updateSupervisionSchedule(eo);
		this.updateByPrimaryKeySelective(eo);
	}

	@Override
	public PagedResult<SupervisionScheduleEO> getVOPage(SupervisionScheduleEO vo) {
		Integer pageNo = vo.getPageNo() == null ? 1 : vo.getPageNo();
		Integer pageSize = vo.getPageSize() == null ? 15 : vo.getPageSize();
		PageHelper.startPage(pageNo, pageSize);
		List<SupervisionScheduleEO> list = supervisionScheduleMapper.selectPagination(vo);
		if (!XTListUtils.isEmpty(list)) {
			List<Long> userIds = new ArrayList<>();
			for (SupervisionScheduleEO supervisionScheduleEO : list) {
				userIds.add(supervisionScheduleEO.getUserId());
			}
			Map<Long, SysUser> userMap = userService.getMapByIdList(userIds);
			if (userMap != null) {
				for (SupervisionScheduleEO supervisionScheduleEO : list) {
					SysUser user = userMap.get(supervisionScheduleEO.getUserId());
					if (user != null) {
						supervisionScheduleEO.setNickname(user.getNickname());
					}
				}
			}
		}

		PagedResult<SupervisionScheduleEO> page = PagedBeanUtil.toPagedResult(list);
		return page;
	}

	@Override
	public SupervisionScheduleEO getInfo(Long id) throws XTBusinessException {
		SupervisionScheduleEO wd = super.getByPrimaryKey(id);
		return wd;
	}

	@Override
	public Integer getScheduleBySupervisionId(SupervisionScheduleEO eo) {
		if (eo == null || eo.getSupervisionId() == null || eo.getUserId() == null) {
			return null;
		}
		return this.supervisionScheduleMapper.selectScheduleBySupervisionId(eo);
	}

	@Override
	public List<SupervisionScheduleEO> getListBySupervisionId(Long supervisionId) {
		return this.supervisionScheduleMapper.selectListBySupervisionId(supervisionId);
	}

	@Override
	public List<SupervisionScheduleEO> getListBySupervisionIds(List<Long> supervisionIds) {
		return this.supervisionScheduleMapper.selectListBySupervisionIds(supervisionIds);
	}

	@Override
	public List<SupervisionScheduleEO> getListByUserId(Long userId) {
		return this.supervisionScheduleMapper.selectListByUserId(userId);
	}

	@Override
	public List<SupervisionScheduleEO> getListBySupervisionIdAndUserId(Long supervisionId, Long userId) {
		return this.supervisionScheduleMapper.selectListBySupervisionIdAndUserId(supervisionId, userId);
	}
}
